using System;
using System.Collections.Generic;
using System.Text;
using System.Xml.Serialization;

namespace G2007.CodeGen.Library.Data
{
    public enum ClassType : int
    {
        single , array
    }

    [Serializable]
    [XmlInclude ( typeof ( ClassType ) )]
    public class ClassAttributes
    {
        string name , type;
        ClassType ctype;
        [XmlIgnore]
        CGObject[] otherClasses;
        [XmlIgnore]
        Enum[] enums;

        string className;
		string dbName;


		string columnKey;

		public string ColumnKey { get { return columnKey; } set { columnKey = value; } }

		private string columnRef;

		public string ColumnRef
		{
			get { return columnRef; }
			set { columnRef = value; }
		}


		private string tableName;

		public string TableName
		{
			get { return tableName; }
			set { tableName = value; }
		}

		private string where;

		public string Where
		{
			get { return where; }
			set { where = value; }
		}

		private bool hasMany;

		public bool HasMany
		{
			get { return hasMany; }
			set { hasMany = value; }
		}

        public string[] Values
        {
            get
            {
                if ( IsEnum )
                {

                    foreach ( Data.Enum e in enums ) { if ( e.Name.Equals ( type ) ) return e.Names; }
                } return new string[0];
            }
            set { }
        }
	
	
	


        bool toString = false;
        bool hidden = false;
        bool compare = false;
		bool key = false;
		bool identity = false;

        bool isNew = false, isRemoved = false;

		private bool isNested;

		public bool IsNested
		{
			get { return isNested; }
			set { isNested = value; }
		}
	

        public bool IsNew { get { return isNew; } set { isNew = value; } }

        public bool IsRemoved { get { return isRemoved; } set { isRemoved = value; } }

		public bool Identity { get { return identity; } set { identity = value; } }

        public ClassAttributes () { }

        public ClassAttributes ( string name , string type , ClassType ctype , CGObject[] otherClasses , 
			Enum[] enums , bool toString , bool hidden , string className , bool compare , bool key , bool identity , string dbName,
			string tableName, string columnRef, string columnKey, string where, bool hasMany, bool isNested )
            : this ()
        {
            this.name = name; this.type = type; this.ctype = ctype; 
			this.otherClasses = otherClasses; this.enums = enums; this.toString = toString;
			this.key = key; this.identity = identity;
            this.compare = compare;
            this.className = className; this.hidden = hidden;
			this.dbName = dbName;

			this.isNested = isNested;

			this.hasMany = hasMany;
			this.columnKey = columnKey;
			this.columnRef = columnRef;
			this.tableName = tableName;
			this.where = where;
            // here needs a 'reserved word' section and a boolean flag if we modify the file for it
        }

		public string DBName { get { return dbName; } set { dbName = value; } }
        public string Name { get { return name.Substring ( 0 , 1 ).ToLower () + name.Substring ( 1 , name.Length - 1 ); } set { name = value; } }
        public string LName { get { return Name.ToLower ( ); } set { } }
        public string PropertyName { get { return name.Substring ( 0 , 1 ).ToUpper () + name.Substring ( 1 , name.Length - 1 ); } set { name = value; } }
        public string Type { get { return type.Replace ( "big" , "" ); } set { type = value; } }
        public string LType { get { return Type.ToLower ( ); } set { } }
		public string RawName { get { return name.Replace ( DBConvertor.DBToScript.FIELD_CLASS_NAME_MATCH_EXTENDER,"" ) ; } set { } }
        public bool IsEnum
        {
            get { foreach ( Data.Enum e in enums ) { if ( e.Name.Equals ( type ) ) return true; } return false; }
            set { int p = 0xf; p++; }
        }


        public bool ToString { get { return toString; } set { toString = value; } }
        public bool Hidden { get { return hidden; } set { hidden = value; } }
		public bool Key { get { return key; } set { key = value; } }

        public bool Compare
        {
            get { return compare; }
            set { compare = value; }
        }

        public bool KnownClass
        {
            get
            {
                if ( type.Equals ( className ) ) return true;//because we might have an instance of ourselves inside ourselves (yuck)
                foreach ( CGObject c in otherClasses )
                {
                    if ( c.Name.Equals ( type ) ) return true;
                }
                return false;
            }
            set { int p = 0xf; p++; }

        }
        public ClassType CType { get { return ctype; } set { ctype = value; } }

        public string SqlAccess
        {
            set { int p = 0xf; p++; }
            get
            {
                if ( IsEnum )
                {
                    return "GetInt32(x)";
                }
                // we need a lookup table for this
                int x = 0;
                while ( true )
                {
                    string val = System.Configuration.ConfigurationSettings.AppSettings[ "SQLVAL[" + x + "]" ];
                    if ( val == null || val == "" ) return "GetGuid(x)";

                    if ( val.Split ( '|' )[ 0 ].Equals ( type ) )
                    {
                        if ( val.Split ( '|' ).Length > 1 ) return val.Split ( '|' )[ 1 ];
                        else return "";
                    }
                    x++;
                }
            }
        }

        public string SqlType
        {
            set { int p = 0xf; p++; }
            get
            {
                if ( IsEnum )
                {
                    return "[int]";
                }
                int x = 0;
                while ( true )
                {
                    string val = System.Configuration.ConfigurationSettings.AppSettings[ "SQLDBVAL[" + x + "]" ];
                    if ( val == null || val == "" ) return "uniqueidentifier";

                    if ( val.Split ( '|' )[ 0 ].Equals ( type ) )
                    {
                        if ( val.Split ( '|' ).Length > 1 ) return val.Split ( '|' )[ 1 ];
                        else return "";
                    }
                    x++;
                }

            }
        }

        public string SqlSPType
        {
            set { int p = 0xf; p++; }
            get
            {
                if ( IsEnum )
                {
                    return "int";
                }
                int x = 0;
                while ( true )
                {
                    string val = System.Configuration.ConfigurationSettings.AppSettings[ "SQLDBVAL[" + x + "]" ];
                    if ( val == null || val == "" ) return "uniqueidentifier"; // this might need to return objectID

                    if ( val.Split ( '|' )[ 0 ].Equals ( type ) )
                    {
                        if ( val.Split ( '|' ).Length > 2 ) return val.Split ( '|' )[ 2 ];
                        else return "";
                    }
                    x++;
                }

            }
        }
    }


}
